<?php
declare (strict_types = 1);

namespace app\common\model\mysql;

/**
 * @mixin \think\Model
 */
class Module extends BaseModel
{
    /**
     * 获取符合条件的所有栏目
     *
     * @param array $where
     * @return Module[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getLists(array $condition = [])
    {
        $order  = ['listorder' => 'desc', 'id' => 'desc'];
        $result = $this->where('status', '<>', config('status.mysql.table_delete'))
                        ->where($condition)
                        ->order($order)
                        ->select();
        return $result;
    }

    /**
     * 通过 PID 获取子级栏目个数
     *
     * @param array $pidArr
     * @param string $field
     * @return array
     */
    public function getChildCountByPid(array $pidArr = [], string $field = "pid, count(*) as count")
    {
        if (!$pidArr) return [];
        $where = [['status', '<>', config('status.mysql.table_delete')]];
        $result = $this->field($field)->whereIn('pid', $pidArr)->where($where)->group('pid')->select();
        return $result->toArray();
    }

    /**
     * 获取所有状态正常的栏目数据
     *
     * @param bool $field
     * @return Module[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getNormalData($field = true, array $condition = [])
    {
        $condition['status'] = config('status.mysql.table_normal');
        $order = ['listorder' => 'desc', 'id' => 'desc'];
        $result = $this->field($field)->where($condition)->order($order)->select();
        return $result;
    }
}
